Thread: Repetition in do{}while

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    93

    Repetition in do{}while

    I googled for this stupid error, and have tried certain things, but can't figure out how to avoid the repetition of my main prompt in a do while loop.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define MAX_SCORE  100
    #define MIN_SCORE  0
    #define TOTAL_ASSIGNMENT_MAX_SCORE 300
    
    void menu (void);
    float take_score (void);
    float final_score_calc (float x, float y, float z);
    char letter_grade (float x);
    float G_P_A (char x);
    void info_display (void);
    void print_scores (float x, char y, int z);
    
    int main () {
    
    //int total_assignments, midterm, final_exam, students, counter,GPA;
    //int A_Count=0, B_Count=0, C_Count=0, D_Count=0, F_Count=0, total_Grade_Count;
    //float final_score, total_final_score=0, A_Percentage, B_Percentage, C_Percentage, D_Percentage, F_Percentage;
    
    menu();
    return 0;
    
    
    
    /*
     total_final_score /= students;
     total_Grade_Count = A_Count + B_Count + C_Count + D_Count + F_Count;
    
     A_Percentage = (( float ) ACounter / total_Grade_Count) * 100;
     B_Percentage = (( float ) BCounter / total_Grade_Count) * 100;
     C_Percentage = (( float ) CCounter / total_Grade_Count) * 100;
     D_Percentage = (( float ) DCounter / total_Grade_Count) * 100;
     F_Percentage = (( float ) FCounter / total_Grade_Count) * 100;
    
     printf("\nStatistical result of your class:\n\n");
     printf("\n\tTotal Students Number:\t%d", students);
     printf("\n\tAverage score:\t%.2f",total_final_score);
     printf("\n\tTotal A grade: %d, percentage   %.2f%%", A_Count, A_Percentage);
     printf("\n\tTotal B grade: %d, percentage   %.2f%%", B_Count, B_Percentage);
     printf("\n\tTotal C grade: %d, percentage   %.2f%%", C_Count, C_Percentage);
     printf("\n\tTotal D grade: %d, percentage   %.2f%%", D_Count, D_Percentage);
     printf("\n\tTotal F grade: %d, percentage   %.2f%%\n", F_Count, F_Percentage);
    */
    }
    
    void menu (void) {
    
    	char c;
    	float final_score, total_final_score=0, average_final_score=0, high_score=-1, low_score=101;
    	float A_Percentage, B_Percentage, C_Percentage, D_Percentage, F_Percentage;
    	char grade;
    	int GPA, student_counter=0, ACounter=0, BCounter=0, CCounter=0, DCounter=0, FCounter=0, total_Grade_Count=0;
    
    	total_Grade_Count = ACounter + BCounter + CCounter + DCounter + FCounter;
    
    	A_Percentage = (( float ) ACounter / total_Grade_Count) * 100;
    	B_Percentage = (( float ) BCounter / total_Grade_Count) * 100;
    	C_Percentage = (( float ) CCounter / total_Grade_Count) * 100;
    	D_Percentage = (( float ) DCounter / total_Grade_Count) * 100;
    	F_Percentage = (( float ) FCounter / total_Grade_Count) * 100;
    	
    	
    	do{
    
    
    	printf("A/a  - Add a student's record\n");
    	printf("D/d - Display current statistical results\n");
    	printf("Q/q - Quit\n");
    	printf("\nPlease enter a key for one of the above options:");
    
    		c=getchar();
    		c=toupper(c);
    
    		switch (c) {
    
    		case 'A': 
    			
    				  final_score = take_score();
    				  grade = letter_grade(final_score);
    				  GPA = G_P_A(grade);
    				  print_scores(final_score, grade, GPA);
    
    				  student_counter ++;
    
    				  switch (grade) {
    
    				  case 'A': ACounter++;
    								break;
    
    				  case 'B': BCounter++;
    								break;
    
    				  case 'C': CCounter++;
    								break;
    
    				  case 'D': DCounter++;
    								break;
    
    				  case 'F': FCounter++;
    								break;
    
    				  }
    
    				  total_final_score += final_score;
    
    				  average_final_score = (total_final_score / student_counter);
    
    				  if (final_score > high_score)
    					  high_score = final_score;
    
    				  if (final_score < low_score)
    					  low_score = final_score;
    					  
    				
    				  break;
    
    		case 'D':  
    			
    				   printf("\nStatistical result of your class:\n\n");
    				   printf("\n\tTotal Students Number:\t%d", student_counter);
    				   printf("\n\tAverage score:\t%.2f",average_final_score);
    				   printf("\n\tHigh score:\t%.2f",high_score);
    				   printf("\n\tLow score:\t%.2f",low_score);
    				   printf("\n\tTotal A grade: %d, percentage   %.2f%%", ACounter, A_Percentage);
    				   printf("\n\tTotal B grade: %d, percentage   %.2f%%", BCounter, B_Percentage);
    				   printf("\n\tTotal C grade: %d, percentage   %.2f%%", CCounter, C_Percentage);
    			       printf("\n\tTotal D grade: %d, percentage   %.2f%%", DCounter, D_Percentage);
    				   printf("\n\tTotal F grade: %d, percentage   %.2f%%\n\n", FCounter, F_Percentage);
    				  
    				   break;
    
    		}
    
    	}while (c != 'Q');
    
    	printf("\nBye!\n\n");
    
    }
    
    float final_score_calc (float x, float y, float z) {
    
    	 float final_score;
    
    	 final_score = x /3.0* 0.3 + y * 0.3+ z* 0.4;
    
    	 return final_score;
    
     }
    
    float take_score (void) {
    
    	float total_assignments, midterm, final_exam, final_score;
    
      printf("\nEnter a student's total assignment, midterm and final exam scores:");
      scanf("%f %f %f", &total_assignments, &midterm, &final_exam);
    
      while (total_assignments > TOTAL_ASSIGNMENT_MAX_SCORE || total_assignments < MIN_SCORE)
    
      {
       printf("\nPlease re-enter total assignment score (0~300) :");
       scanf("%f", &total_assignments);
      }
    
    
      while (midterm > MAX_SCORE || midterm < MIN_SCORE)
    
      {
       printf("\nPlease re-enter midterm score (0~100) :");
       scanf("%f", &midterm);
      }
    
      while (final_exam > MAX_SCORE || final_exam <MIN_SCORE)
    
      {
       printf("\nPlease re-enter final exam score (0~100) :");
       scanf("%f", &final_exam);
      }
     
      final_score = final_score_calc (total_assignments, midterm, final_exam);
    
      return final_score;
    
     }
    
    char letter_grade (float x) {
    
    	float final_score = x;
    	
    	char grade;
    
    	if (final_score >= 90)
    		grade='A';
    
    	else if (final_score < 90 && final_score >= 80)
    		grade='B';
    
    	else if (final_score < 80 && final_score >=70)
    		grade='C';
    
    	else if (final_score < 70 && final_score >= 60)
    		grade='D';
    
    	else if (final_score < 60)
    		grade='F';
    
    	return grade;
    
    }
    
    float G_P_A (char x) {
    	
    
    	float GPA;
    
    	switch ( x ) {
     case 'A': GPA = 4; 
          break;
    
     case 'B': GPA = 3; 
          break;
    
     case 'C': GPA = 2; 
          break;
    
     case 'D': GPA = 1; 
          break;
    
     case 'F': GPA = 0; 
          break;
    
     }
    
    	return GPA;
    
    }
    
    void print_scores (float x, char y, int z) {
    
    	printf("final_score:   %.2f  letter grade:%c  GPA:%d\n\n", x, y, z);
    
    }
    When you compile it you will probably notice that the high/low scores and the percentages need work, but I am still working on those. I should be able to figure them out, but I don't know what to do with the do-while.

    Any hints, or pointers in the right direction would be extremely helpful.

    Thanks.
    Last edited by Beast(); 06-15-2004 at 11:58 PM. Reason: Spelled repetition like a bum

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sentinel controlled repetition example
    By droseman in forum C Programming
    Replies: 7
    Last Post: 09-26-2008, 02:17 AM
  2. repetition with the "for" structure
    By Lillian in forum C Programming
    Replies: 3
    Last Post: 10-19-2002, 09:28 PM
  3. Repetition of a line
    By niroopan in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2002, 04:44 PM
  4. Controlling Repetition in a Structure
    By Silence in forum C Programming
    Replies: 2
    Last Post: 08-23-2002, 05:35 PM
  5. repetition and selection- can anyone help?
    By Tarls in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2002, 10:18 PM